package com.itheima.service.impl;
import	java.lang.annotation.Target;
import java.util.List;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 代码使我快乐 Administrator on 2019/11/30.
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl  implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult queryPage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage,pageSize);

        String queryString = queryPageBean.getQueryString();
        if(queryString != null){

            queryString = "%"+queryString+"%" ;

            System.out.println(queryString);

            queryPageBean.setQueryString(queryString);
        }

        List<CheckItem> checkItems = checkItemDao.queryPage(queryPageBean);
        PageInfo<CheckItem> pageInfo = new PageInfo<>(checkItems);
        long total = pageInfo.getTotal();

//        Page<CheckItem> page = checkItemDao.queryPage(queryPageBean);
//        long total = page.getTotal();
//        List<CheckItem> rows = page.getResult();
        return new PageResult(total,checkItems);
    }

    @Override
    public void deleteCheck(Integer id) {
        long countCheckItem = checkItemDao.findCountCheckItem(id);
        if(countCheckItem > 0){
//            有表关联不能删除
            new RuntimeException();
        }else {
            checkItemDao.deleteCheck(id);
        }
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    @Override
    public CheckItem findById(Integer id) {

       CheckItem checkItem = checkItemDao.findById(id);
        return checkItem;
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }


}
